package org.domeos.framework.api.biz.deployment.impl;

import org.domeos.framework.api.biz.base.impl.BaseBizImpl;
import org.domeos.framework.api.biz.deployment.DeploymentBiz;
import org.domeos.framework.api.mapper.domeos.deployment.DeploymentMapper;
import org.domeos.framework.api.model.deployment.Deployment;
import org.domeos.framework.api.model.deployment.related.DeploymentType;
import org.domeos.framework.engine.exception.DaoException;
import org.domeos.framework.engine.model.RowMapperDao;
import org.domeos.global.GlobalConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by xxs on 15/12/19.
 */
@Service("deploymentBiz")
public class DeploymentBizImpl extends BaseBizImpl implements DeploymentBiz {
    private static Logger logger = LoggerFactory.getLogger(DeploymentBizImpl.class);

    @Autowired
    DeploymentMapper mapper;

    @Override
    public void createDeployment(Deployment deployment) {
        mapper.insertDeploy(deployment, deployment.toString());
    }

    @Override
    public Deployment getDeployment(int id) {
        Deployment deployment = super.getById(GlobalConstant.DEPLOY_TABLE_NAME, id, Deployment.class);
        if (deployment != null && deployment.getDeploymentType() == null) {
            deployment.setDeploymentType(DeploymentType.REPLICATIONCONTROLLER);
        }
        return deployment;
    }

    @Override
    public void update(Deployment deployment) throws DaoException {
        mapper.updateDeploy(new RowMapperDao(deployment));
    }

    @Override
    public void updateDescription(int id, String description) {
        mapper.updateDescription(id, description);
    }

    @Override
    public List<Deployment> listDeploymentByClusterId(int id) {
        List<Deployment> deployments = new LinkedList<>();
        for (RowMapperDao dao : mapper.listDeploymentByClusterId(id)) {
            try {
                deployments.add(super.checkResult(dao, Deployment.class));
            } catch (Exception e) {
                logger.warn("deployment map error, " + dao.toString());
            }
        }
        return deployments;
    }

    @Override
    public List<Deployment> getDeployment(int clusterId, String deployName) {
        List<Deployment> deployments = new LinkedList<>();
        for (RowMapperDao dao : mapper.getDeployment(clusterId, deployName)) {
            try {
                deployments.add(super.checkResult(dao, Deployment.class));
            } catch (Exception e) {
                logger.warn("deployment map error, " + dao.toString());
            }
        }
        return deployments;
    }
    
    @Override
    public List<Deployment> getRemoveDeployment(int clusterId, String deployName) { 	
        List<Deployment> deployments = new LinkedList<>();
        for (RowMapperDao dao : mapper.getRemoveDeployment(clusterId, deployName)) {
            try {
//                deployments.add(super.checkResult(dao, Deployment.class));
            	deployments.add(dao.toModel(Deployment.class));
            } catch (Exception e) {
                logger.warn("deployment map error, " + dao.toString());
            }
        }
        return deployments;
    }

    @Override
    public List<Deployment> listUnfinishedStatusDeployment() {
        return mapper.listUnfinishedStatusDeployment();
    }

    @Override
    public List<Deployment> listRunningDeployment() {
        return mapper.listRunningDeployment();
    }
}
